from typing import List


class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        intervals.append(newInterval)
        intervals.sort(key=lambda x: x[0])

        # 合并区间
        def merge(a):
            st = []
            for x in a:
                if not st or st[-1][1] < x[0]:
                    st.append(x)
                else:
                    # 合并
                    cur = st.pop()
                    st.append([cur[0], max(cur[1], x[1])])
            return st

        return merge(intervals)

